<template>
  <Editor
    :id="id"
    :title="title"
    :content="searchText"
    :type="type"
    theme="vs-dark"
    @update:moment="(id, content, title) => updateMomentContent(id, content, title)"
  />
</template>

<style scoped>
html, body {
  background-color: #1e1e1e; /* 或 transparent / black，根据你的 UI */
  margin: 0;
  padding: 0;
}

</style>

<script setup lang="ts">
    import { onMounted, ref, watch } from 'vue'
    import {  getCurrentWindow } from '@tauri-apps/api/window';
    import { invoke } from '@tauri-apps/api/core';
    import Editor from '../components/editor.vue'

    const appWindow = getCurrentWindow();
    let id = ref("");
    let title = ref("");
    const type = ref("text");
    const searchText = ref('')
    const setTitle = ref(false);
    const isSetTitle = ref(false);

    onMounted(async () => {
        await appWindow.show();
        try {
            title.value = await appWindow.title();
            let split = title.value.split("-");
            if (split.length == 2) {
                setTitle.value = true;
                title.value = split[0].trim();
                type.value = split[1];
            }

            const label = appWindow.label;
            console.log("label: " + label);
            
            if (label && label.startsWith("editor-")) {
              const eid = label.replace("editor-", "");
              console.log("eid: " + eid);
              if (eid) {
                let data:any = await invoke("get_info_by_id", { id: eid });
                if (data.is_success) {
                  id.value = eid;
                  searchText.value = data.data.note_content;
                  setTitle.value = true;
                  title.value = data.data.title;
                  id = data.data.id;
                }
              }
            }
            await appWindow.setTitle(title.value)
        } catch (error) {
            console.error('获取title：', error);
        }
    })

    watch(searchText, async (_n,_o) => {
      if (!setTitle.value && searchText.value && /[\r\n]/.test(searchText.value)) {
          let title = processText(searchText.value, 5, "");
          console.log(title);
          if (title && !isSetTitle.value) {
              setTitle.value = true;
              await appWindow.setTitle(title);  // 获取前3个字符
          }
      }
    })

    function processText(text: string, maxLen: number, defaultValue: string): string {
      // 去除回车换行符
      const cleaned = text.replace(/[\r\n]/g, '');
      // 如果清理后是空字符串，返回默认值
      if (cleaned.trim() === '') {
          return defaultValue;
      }
      // 返回不超过 maxLen 的有效子串
      return cleaned.slice(0, Math.min(cleaned.length, maxLen));
    }



  async function updateMomentContent(id: string, content: string, title: string) {
    try {
      let data:any = await invoke("update_momento_content", { id: id, noteContent: content, title: title, openType: type.value});
      if (data.is_success) {
      }
      console.log('后台已更新为:', content)
    } catch (e) {
      console.error(`invoke 调用失败`, e)
    }
  }

</script>

